﻿#include "battery_kettle_broken.h"

// 使用 utf8 编码
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

// 动力电池水壶破损检测

/// <summary>
/// 
/// </summary>
battery_kettle_broken::battery_kettle_broken()
{
	m_pBatteryDet = nullptr;
	m_pKettleCls = nullptr;
}


/// <summary>
/// 
/// </summary>
battery_kettle_broken::~battery_kettle_broken()
{

	if (m_pBatteryDet != nullptr)
	{
		m_pBatteryDet->Release();
		m_pBatteryDet = nullptr;
	}
	if (m_pKettleCls != nullptr)
	{
		m_pKettleCls->Release();
		m_pKettleCls = nullptr;
	}
}


/// <summary>
/// 
/// </summary>
/// <returns></returns>
void battery_kettle_broken::init()
{
	m_pBatteryDet = CreateDetObj(m_strKettleDetModelPath);
	if (m_pBatteryDet != nullptr)
	{
		m_pBatteryDet->setScore(0.9);
	}
	m_pKettleCls = CreateClsObj(m_strKettleRectDefectModelPath);
}


/// <summary>
/// 
/// </summary>
/// <param name="pData"></param>
void battery_kettle_broken::execute(QSharedPointer<IMG_DATA> pData, int nBuffersize)
{
	IMG_DATA data = *pData;
	data.image = urlToPixmap(data.strUrl);
	saveImage(data);

	cv::Mat image = QImage2cvMat(data.image);
	int nDispId = data.nDispId;

	if (!image.data || m_pBatteryDet == nullptr)
	{
		return;
	}


	std::vector<DetectResult> results;
	m_pBatteryDet->Evaluate(image, results);
	// 保存图片
	saveImage(image, "水壶(定位)", data, results.size());

	int nClass = -1;
	for (int i = 0; i < results.size(); i++)
	{
		DetectResult res = results[i];
		cv::Rect rect = res.box;
		rect &= cv::Rect(0, 0, image.cols, image.rows);//求交集
		cv::Mat image_roi = image(rect);
		cv::resize(image_roi, image_roi, cv::Size(160,128 ), cv::INTER_LINEAR);

		std::vector<int> rstClassLis;
		if (m_pKettleCls!= nullptr)
		{
			std::vector<cv::Mat> srcImg;
			srcImg.push_back(image_roi);

			std::vector<int> rstClassLis; 
			std::vector<float> rstProbList;
			m_pKettleCls->Evaluate(srcImg, rstClassLis, rstProbList);

			if (rstClassLis.size() >0)
			{
				nClass = rstClassLis[0];
			}
		}

		// 如果没检测到破损，则不发布破损相关的数据
		if (nClass == 1)
		{
			// 返回结果到 kafka 
			_tagResults tagResults;
			tagResults.setImageData(data);
			tagResults.setResults("水壶破损", results);
			tagResults.setVersion(m_strKettleRectDefectModelPath);

			QString strResultTopc = data.strTopicName + suffix_2;
			QString strResultData = tagResults.toJSon();

			pushKafka(strResultTopc, strResultData, data.strStationId);
		}
		
		QPainter painter(&data.image);
		if (nClass == 0)
		{
			painter.setBrush(QBrush(QColor(0, 255, 0, 50)));
			QPen pen(QColor(0, 255, 0));
			pen.setWidth(5);
			painter.setPen(pen);
		}
		else
		{
			painter.setBrush(QBrush(QColor(255, 0, 0, 50)));
			QPen pen(Qt::red);
			pen.setWidth(5);
			painter.setPen(pen);
		}

		saveImage(image_roi, "水壶破损", data, nClass);

		cv::Point point = getCenterPoint(rect);
		painter.drawRect(rect.tl().x, rect.tl().y, rect.width, rect.height);

		emit updateShow(data.nDispId, data.image, nBuffersize);
	}



	// 返回结果到 kafka 
	_tagResults tagResults;
	tagResults.setImageData(data);
	tagResults.setResults("水壶", results);
	tagResults.setVersion(m_strKettleDetModelPath);

	QString strResultTopc = data.strTopicName + suffix_1;
	QString strResultData = tagResults.toJSon();

	pushKafka(strResultTopc, strResultData, data.strStationId);
	//mutex.unlock();

}
